package design.graduation.novelwebsite.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import design.graduation.novelwebsite.constant.Constant;
import design.graduation.novelwebsite.model.dto.ResultJson;
import design.graduation.novelwebsite.model.entity.BookContent;
import design.graduation.novelwebsite.model.entity.IncludeBook;
import design.graduation.novelwebsite.service.BookContentService;
import design.graduation.novelwebsite.service.IncludeBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 洪少尉
 * @Date: 2020/3/28 20:05
 * @Description:
 */
@RestController
@CrossOrigin
@RequestMapping("detail")
public class DetailPageController {
	@Autowired
	private BookContentService bookContentService;
	@Autowired
	private IncludeBookService includeBookService;
	@RequestMapping("getChapter/{bookId}")
	public ResultJson getChapter(@PathVariable String bookId){
		QueryWrapper<BookContent> queryWrapper = new QueryWrapper<BookContent>().select(BookContent.COL_ID, BookContent.COL_BOOK_ID,BookContent.COL_BOOK_NAME,BookContent.COL_CHAPTER_NAME,BookContent.COL_CHAPTER_ID);
		queryWrapper.eq(BookContent.COL_AVAILABLE, Constant.AVAILABLE_TRUE)
				.eq(BookContent.COL_BOOK_ID,bookId);
		IncludeBook book = this.includeBookService.getById(bookId);
		book.setMonthClicks(book.getMonthClicks()+1);
		book.setTotalClicks(book.getTotalClicks()+1);
		List<BookContent> list = this.bookContentService.list(queryWrapper);
		this.includeBookService.updateById(book);
		return new ResultJson(Constant.SUCCESS_CODE,"成功",(long)list.size(),list);
	}
	@RequestMapping("getContent/{bookContentId}")
	public ResultJson getContent(@PathVariable String bookContentId){
		BookContent bookContent = this.bookContentService.getById(bookContentId);
		return new ResultJson(Constant.SUCCESS_CODE,"成功",bookContent);
	}
	@RequestMapping("getBook/{id}")
	public ResultJson getBook(@PathVariable String id){
		return new ResultJson(Constant.SUCCESS_CODE,"成功",this.includeBookService.getById(id));
	}
	@RequestMapping("getBookId/{id}")
	public ResultJson getBookId(@PathVariable String id){
		return new ResultJson(Constant.SUCCESS_CODE,"成功",this.bookContentService.getById(id));
	}

}
